package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.WantBuyOrder;
import com.sc.nft.entity.vo.WantBuyOrderWebPageVO;
import com.sc.nft.enums.SecondWantBuyOrderStatusEnum;
import com.sc.nft.enums.SecondWantBuyOrderTypeEnum;
import com.sc.nft.mapper.WantBuyOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * @author 刘国航
 * @desc
 * @since 2024/12/19 20:01
 */
@Slf4j
@Repository
public class WantBuyOrderDao extends ServiceImpl<WantBuyOrderMapper, WantBuyOrder> {

    public Page<WantBuyOrder> page(Page page, Long userId, SecondWantBuyOrderStatusEnum status) {
        LambdaQueryWrapper<WantBuyOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(userId), WantBuyOrder::getUserId, userId);
        queryWrapper.eq(ObjectUtil.isNotNull(status), WantBuyOrder::getStatus, status);
        queryWrapper.orderByDesc(WantBuyOrder::getStartTime);
        return page(page, queryWrapper);
    }

    public IPage<WantBuyOrder> page(Page page, SecondWantBuyOrderTypeEnum orderType, Long relationId, Long excludeUserId) {
        LambdaQueryWrapper<WantBuyOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(WantBuyOrder::getUserId, excludeUserId);
        queryWrapper.eq(WantBuyOrder::getOrderType, orderType);
        queryWrapper.eq(WantBuyOrder::getRelationId, relationId);
        queryWrapper.eq(WantBuyOrder::getStatus, SecondWantBuyOrderStatusEnum.RELEASE);
        queryWrapper.gt(WantBuyOrder::getEndTime, DateTime.now());
        queryWrapper.gt(WantBuyOrder::getUsableWantBuyQuantity, 0);
        queryWrapper.orderByAsc(WantBuyOrder::getWantBuyPrice);
        queryWrapper.orderByAsc(WantBuyOrder::getStartTime);
        return page(page, queryWrapper);
    }

    public IPage<WantBuyOrder> page(Page page, Long userId, SecondWantBuyOrderTypeEnum orderType, Long relationId) {
        LambdaQueryWrapper<WantBuyOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WantBuyOrder::getUserId, userId);
        queryWrapper.eq(WantBuyOrder::getOrderType, orderType);
        queryWrapper.eq(Objects.nonNull(relationId), WantBuyOrder::getRelationId, relationId);
        queryWrapper.orderByDesc(WantBuyOrder::getStartTime);
        return page(page, queryWrapper);
    }

    public List<WantBuyOrder> releaseList(SecondWantBuyOrderTypeEnum orderType, Long relationId) {
        LambdaQueryWrapper<WantBuyOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WantBuyOrder::getOrderType, orderType);
        queryWrapper.eq(WantBuyOrder::getRelationId, relationId);
        queryWrapper.eq(WantBuyOrder::getStatus, SecondWantBuyOrderStatusEnum.RELEASE);
        queryWrapper.gt(WantBuyOrder::getEndTime, DateTime.now());
        queryWrapper.gt(WantBuyOrder::getUsableWantBuyQuantity, 0);
        return list(queryWrapper);
    }

    /**
     * 非共识区
     *
     * @param page
     * @param orderType
     * @param relationId
     * @return
     */
    public IPage<WantBuyOrderWebPageVO> exitConsensusPage(Page page, SecondWantBuyOrderTypeEnum orderType, Long relationId, Long excludeUserId) {
        return baseMapper.exitConsensusPage(page, orderType.getCode(), relationId, excludeUserId);
    }

    /**
     * 共识区
     *
     * @param page
     * @param orderType
     * @param relationId
     * @return
     */
    public IPage<WantBuyOrderWebPageVO> consensusPage(Page page, SecondWantBuyOrderTypeEnum orderType, Long relationId, Long excludeUserId) {
        return baseMapper.consensusPage(page, orderType.getCode(), relationId, excludeUserId);
    }

}
